package com.lyubia.shop.sso.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lyubia.shop.api.pojo.Admin;
import com.lyubia.shop.api.pojo.AdminExample;
import com.lyubia.shop.api.service.SSOService;
import com.lyubia.shop.common.exception.ParamsException;
import com.lyubia.shop.common.utils.AssertUtil;
import com.lyubia.shop.common.utils.Md5Util;
import com.lyubia.shop.common.utils.UUIDUtil;
import com.lyubia.shop.sso.mapper.AdminMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service(version = "1.0", interfaceClass = SSOService.class)
public class SSOServiceImpl implements SSOService {

    @Resource
    private AdminMapper adminMapper;

    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOperations;

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${user.ticket}")
    private String userTicket;

    @Override
    public String login(String username, String password) {
        checkParam(username,password);
        AdminExample example = new AdminExample();
        example.createCriteria().andUserNameEqualTo(username);
        List<Admin> admins = adminMapper.selectByExample(example);
        if (CollUtil.isEmpty(admins)) {
            throw new ParamsException("用户信息不存在!");
        }
        Admin temp = null;
        for (Admin admin : admins) {
            String pwd = Md5Util.getMd5WithSalt(password, admin.getEcSalt());
            if (pwd.equals(admin.getPassword())) {
                temp = admin;
                break;
            }
        }
        if (temp == null) {
            throw new RuntimeException("用户密码错误!");
        }
        String ticket = UUIDUtil.getUUID();
        valueOperations.set(userTicket+":"+ticket, temp,30, TimeUnit.MINUTES);
        return ticket;
    }

    private void checkParam(String username, String password) {
        AssertUtil.isTrue(StrUtil.isBlank(username),"用户名不能为空!");
        AssertUtil.isTrue(StrUtil.isBlank(password),"密码不能为空!");
    }


    @Override
    public Admin checkTicket(String ticket) {
        if (StrUtil.isEmpty(ticket)) {
            throw new ParamsException("登录凭证为空!");
        }
        String redisKey = userTicket + ":" + ticket;
        Admin admin = (Admin) valueOperations.get(redisKey);
        if (admin == null)
            throw new ParamsException("凭证对应用户信息不存在或已过期!");
        return admin;
    }

    @Override
    public void logout(String ticket) {
        String redisKey = userTicket + ":" + ticket;
        if (!redisTemplate.hasKey(redisKey))
            throw new RuntimeException("凭证不合法!");
        redisTemplate.delete(redisKey);
    }

    @Override
    public void freshTicket(String ticket, Admin admin) {
        String redisKry = userTicket + ":" + ticket;
        valueOperations.set(redisKry,admin,30,TimeUnit.MINUTES);
    }
}
